package org.zhangchao.poi.excel.common;

/**
 * 数字进制处理工具类
 * 
 * @author zhangchao
 * 
 */
public final class NumberSystem {

	/**
	 * 把10进制数字转成26进制字符串（从1开始算起）
	 * @param n
	 * @return
	 */
	public final static String ToNumberSystem26(int n) {
		String s = "";
		while (n > 0) {
			int m = n % 26;
			if (m == 0) {
				m = 26;
			}
			s = (char) (m + 64) + s;
			n = (n - m) / 26;
		}
		return s;
	}

	/**
	 * 把26进制字符串转成10进制数字
	 * @param s
	 * @return
	 */
	public final static int FromNumberSystem26(String s) {
		int n = 0;
		if (null != s && !"".equals(s)) {
			char[] cs = s.toUpperCase().toCharArray();
			for (int i = (cs.length - 1), j = 1; i >= 0; i--, j *= 26) {
				char c = (char) cs[i];
				if ('A' < c && c < 'Z') {
					n += ((int) c - 64) * j;
				} else {
					n = 0;
					break;
				}
			}
		}
		return n;
	}
}
